package com.xh.bussiness.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xh.bussiness.system.model.IconSubject;
import com.xh.bussiness.system.service.IIconService;
import com.xh.bussiness.system.model.Icon;
import com.xh.bussiness.system.bean.request.IconRequest;
import com.xh.bussiness.system.service.IIconSubjectService;
import com.xh.core.properties.upload.qiniu.QiniuProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 图标 前端控制器
 * </p>
 *
 * @author zwb
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/admin/icon")
public class IconController extends BaseController {

    @Autowired
    private IIconService iconService;
    @Autowired
    private IIconSubjectService subjectService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(XhPage page,IconRequest bean){
        page=iconService.queryIconPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 根据学科分页查询
     * @author 赵文博
     * @date 2021/6/25 15:05
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/queryIconPage")
    public XhPage queryIconPage(XhPage xhPage,IconRequest bean){
        XhPage page = iconService.queryIconPageBySubject(xhPage,bean);
        return page;
    }
    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(IconRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",iconService.queryIcon(bean));
    }


    /**
     * 根据学科查询图标
     * @author 赵文博
     * @date 2021/6/25 14:49
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/queryIcon")
    public AjaxResult queryIconBySubject(IconRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",iconService.queryIconBySubject(bean));
    }


    /**
     * 新增和保存
     * @param icon 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@RequestBody Icon icon){
        if(icon.getId()==null){
            iconService.insert(icon);
        }else{
            iconService.update(icon);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 新增和保存
     * @param icon 实体对象
     * @return
     */
    @PostMapping(value="/off")
    public AjaxResult off(String ids,Integer status){
        iconService.off(ids,status);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量插入
     * @author 赵文博
     * @date 2021/6/4 14:36
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/batchInsert")
    public AjaxResult batchInsert(@RequestBody List<Icon> icons){
        boolean flag = iconService.batchInsertIcon(icons);
        return flag ? new AjaxResult(AjaxResultConstant.Result.SUCCESS)
                :new AjaxResult(AjaxResultConstant.Result.ERROR);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        iconService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String ids){
        String[] id = ids.split(",");
        iconService.batchDelete(id);
        QueryWrapper<IconSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("icon_id",id);
        subjectService.deleteByCondition(queryWrapper);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 添加图标
     * @author 赵文博
     * @date 2021/6/24 15:00
     * @param iconRequests
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/add")
    public AjaxResult add(@RequestBody List<IconRequest> iconRequests){
        iconService.InsertIcon(iconRequests);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 修改状态
     * @author 赵文博
     * @date 2021/6/24 18:04
     * @param icons
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody List<IconRequest> icons){
        iconService.updateIcon(icons);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

